#include "ui_home_page.h"
#include "lvgl.h"
#include "stdint.h"
#include "usr_objx/gauge.h"
#include "ui_app.h"
#include "driver.h"

#define RUN_MAN_FONT_VAL    "\xEE\x99\xB7"
static lv_color_t hight_color[HIGHT_VIEW_COLOR_GROUP_NUM][2] = 
{
    {LV_COLOR_MAKE(122,157,249), LV_COLOR_MAKE(178, 96, 174)},
};

static lv_task_t *main_task = NULL;
static lv_obj_t *main_view = NULL;
static lv_style_t main_background_style;

static lv_style_t tittle_background_style;
static lv_style_t tittle_date_label_style;
static lv_style_t tittle_battery_label_style;
static lv_obj_t *tittle_layout = NULL;
static lv_obj_t *tittle_date_label = NULL;
static lv_obj_t *tittle_battery_label = NULL;

static lv_style_t time_disp_background_style;
static lv_style_t time_disp_label_style;
static lv_obj_t *time_layout = NULL;
static lv_obj_t *time_disp_label = NULL;


static lv_style_t function_background_style;
static gauge_t pedometer;
static lv_obj_t *function_layout;
static lv_obj_t *height_disp_roller;
static lv_obj_t *pedometer_label;

static page_cmd_t home_page_cmd;

void set_date(uint16_t y, uint8_t m, uint8_t d)
{    
    if(tittle_date_label)
    {
        char * buf = lv_label_get_text(tittle_date_label);
        lv_snprintf(buf, 15, "%04d-%02d-%02d", y, m ,d);
        lv_label_set_static_text(tittle_date_label, buf);
    }

}

void set_time(uint8_t h, uint8_t m)
{    
    if(time_disp_label)
    {
        char * buf = lv_label_get_text(time_disp_label);
        lv_snprintf(buf, 15, "%02d:%02d", h, m);
        lv_label_set_static_text(time_disp_label, buf);
    }

}

void set_voltage(float v)
{    
    if(tittle_battery_label)
    {
        char * buf = lv_label_get_text(tittle_battery_label);
        if(v > 4.0)
        {
            strncpy(buf, LV_SYMBOL_BATTERY_FULL, 15);
        }
        else if(v > 3.5)
        {
            strncpy(buf, LV_SYMBOL_BATTERY_3, 15);
        }
        else if(v > 3)
        {
            strncpy(buf, LV_SYMBOL_BATTERY_2, 15);
        }
        else if(v > 2.5)
        {
            strncpy(buf, LV_SYMBOL_BATTERY_1, 15);
        }
        else
        {
            strncpy(buf, LV_SYMBOL_BATTERY_EMPTY, 15);
        }
        lv_label_set_static_text(tittle_battery_label, buf);
    }
}

void set_height(int height)
{
    if(height_disp_roller)
    {
        static char buf[32];
        lv_snprintf(buf, 64, "%8dm\n%8dm\n%8dm\n", height-1, height, height+1);
        lv_roller_set_options(height_disp_roller, buf, LV_ROLLER_MODE_NORMAL);
        lv_roller_set_selected(height_disp_roller, 1,LV_ANIM_ON);
    }
}

void set_pedometer(uint16_t val)
{
    if(pedometer_label && pedometer.main && pedometer.backgrount)
    {
        char *buf = lv_label_get_text(pedometer_label);
        lv_snprintf(buf, 32, "%s%-4d", RUN_MAN_FONT_VAL, val);
        lv_label_set_static_text(pedometer_label, buf);

        if(val > 10000)
        {
            val = 10000;
        }
        uint16_t angle = (uint16_t)(0.036 * (float)val);
        gauge_set_angle(&pedometer, angle);
    }
}

void tittle_disp_init(lv_obj_t *parent, lv_style_t *style)
{
    if(style)
    {
        lv_obj_t *tittle_layout = lv_cont_create(parent, NULL);
        lv_obj_set_size(tittle_layout, 134, 12);
        lv_cont_set_layout(tittle_layout, LV_LAYOUT_OFF);
        lv_cont_set_style(tittle_layout, LV_CONT_STYLE_MAIN, style);

        tittle_date_label = lv_label_create(tittle_layout, NULL);
        lv_style_copy(&tittle_date_label_style, &lv_style_plain_color);
        tittle_date_label_style.text.font = &date_time_font_6;
        lv_label_set_style(tittle_date_label, LV_LABEL_STYLE_MAIN, &tittle_date_label_style);

        static char date_char_buf[15];
        strncpy(date_char_buf, "2020-01-01", sizeof(date_char_buf));
        lv_label_set_static_text(tittle_date_label, date_char_buf);
        lv_obj_set_user_data(tittle_date_label, (lv_obj_user_data_t)1);
        lv_obj_align(tittle_date_label, tittle_layout, LV_ALIGN_IN_LEFT_MID, 5, 0);

        tittle_battery_label = lv_label_create(tittle_layout, NULL);
        lv_style_copy(&tittle_battery_label_style, &lv_style_plain_color);
        tittle_battery_label_style.text.font = &lv_font_roboto_12;
        lv_label_set_style(tittle_battery_label, LV_LABEL_STYLE_MAIN, &tittle_battery_label_style);

        static char battery_char_buf[15];
        strncpy(battery_char_buf, LV_SYMBOL_BATTERY_EMPTY, sizeof(date_char_buf));
        lv_label_set_static_text(tittle_battery_label, battery_char_buf);
        lv_obj_set_user_data(tittle_battery_label, (lv_obj_user_data_t)2);
        lv_obj_align(tittle_battery_label, tittle_layout, LV_ALIGN_IN_RIGHT_MID, -5, 0);
    }
}

static lv_obj_t *main_create(lv_obj_t *parent, lv_style_t *style)
{
    if(style)
    {
        lv_coord_t height;
        lv_coord_t width;
        lv_obj_t *main_cont = lv_cont_create(parent, NULL);
        height = lv_obj_get_height(parent);
        width  = lv_obj_get_width(parent);
        lv_obj_set_size(main_cont, width, height);
        lv_cont_set_layout(main_cont, LV_LAYOUT_PRETTY);
        lv_cont_set_style(main_cont, LV_CONT_STYLE_MAIN, style);
        return main_cont;
    }
    return NULL;
}


static void time_disp_init(lv_obj_t *parent, lv_style_t *style)
{
    if(style)
    {
        time_layout = lv_cont_create(parent, NULL);
        lv_obj_set_size(time_layout, 122, 63);
        lv_cont_set_layout(time_layout, LV_LAYOUT_CENTER);
        lv_cont_set_style(time_layout, LV_CONT_STYLE_MAIN, style);

        time_disp_label = lv_label_create(time_layout, NULL);
        lv_style_copy(&time_disp_label_style, &lv_style_plain_color);
        time_disp_label_style.text.font = &date_time_font_22;
        lv_label_set_style(time_disp_label, LV_LABEL_STYLE_MAIN, &time_disp_label_style);

        static char time_char_buf[10];
        strncpy(time_char_buf, "00:00", 10);
        lv_label_set_static_text(time_disp_label, time_char_buf);
        lv_obj_set_user_data(time_disp_label, (lv_obj_user_data_t)1);
    }
}


static void function_init(lv_obj_t *parent, lv_style_t *style)
{
    if(style)
    {
        function_layout = lv_cont_create(parent, NULL);
        lv_obj_set_size(function_layout, 122, 144);
        lv_cont_set_layout(function_layout, LV_LAYOUT_CENTER);
        lv_cont_set_style(function_layout, LV_CONT_STYLE_MAIN, style);

        pedometer = gauge_create(function_layout);
        gauge_set_size(&pedometer, 91);
        
        /*高度计*/
        lv_obj_t * height_obj = lv_obj_create(pedometer.main, NULL);
        lv_obj_align_origo(height_obj, pedometer.backgrount, LV_ALIGN_CENTER, 0, 0);
        lv_obj_set_auto_realign(height_obj, true);
        lv_obj_set_user_data(height_obj, (lv_obj_user_data_t)1);

        height_disp_roller = lv_roller_create(height_obj, NULL);
        lv_obj_set_user_data(height_disp_roller, (lv_obj_user_data_t)1);
        lv_obj_set_size(height_obj, 72, 72);
        lv_obj_set_size(height_disp_roller, 72, 72);

        static lv_style_t roller_style[3];
        lv_style_copy(&roller_style[LV_ROLLER_STYLE_BG],
                    lv_roller_get_style(height_disp_roller, LV_ROLLER_STYLE_BG) );

        lv_style_copy(&roller_style[LV_ROLLER_STYLE_SEL],
                    lv_roller_get_style(height_disp_roller, LV_ROLLER_STYLE_SEL) );

        lv_style_copy(&roller_style[2], &lv_style_plain_color);
        roller_style[LV_ROLLER_STYLE_BG].body.radius = 32;
        roller_style[LV_ROLLER_STYLE_BG].body.border.width = 0;
        roller_style[LV_ROLLER_STYLE_BG].body.opa = 0;
        roller_style[LV_ROLLER_STYLE_SEL].body.opa = 0;
        roller_style[LV_ROLLER_STYLE_BG].text.color = LV_COLOR_WHITE;
        roller_style[LV_ROLLER_STYLE_BG].text.opa = 100;
        roller_style[LV_ROLLER_STYLE_BG].text.font = &lv_font_roboto_12;
        roller_style[LV_ROLLER_STYLE_BG].body.padding.left = 16;
        roller_style[LV_ROLLER_STYLE_BG].body.padding.top = 16;
        roller_style[LV_ROLLER_STYLE_BG].body.padding.bottom = 16;
        roller_style[LV_ROLLER_STYLE_SEL].text.color = LV_COLOR_WHITE;
        roller_style[2].body.radius = 36;
        roller_style[2].body.main_color = hight_color[0][1];
        roller_style[2].body.grad_color = hight_color[0][0];
        lv_roller_set_style(height_disp_roller, LV_ROLLER_STYLE_BG, &roller_style[LV_ROLLER_STYLE_BG]);
        lv_roller_set_style(height_disp_roller, LV_ROLLER_STYLE_SEL, &roller_style[LV_ROLLER_STYLE_SEL]);
        lv_obj_set_style(height_obj, &roller_style[2]);
        lv_roller_set_options(height_disp_roller, "0m\n1m\n2m", LV_ROLLER_MODE_NORMAL);
        lv_roller_set_align(height_disp_roller, LV_LABEL_ALIGN_CENTER);

        static lv_style_t run_man_style;
        pedometer_label = lv_label_create(function_layout, NULL);
        lv_style_copy(&run_man_style, &lv_style_plain_color);
        run_man_style.text.font = &run_font_16;
        lv_label_set_style(pedometer_label, LV_LABEL_STYLE_MAIN, &run_man_style);
        static char pedometer_label_buffer[32];
        lv_snprintf(pedometer_label_buffer, 32, "%s%-4d", RUN_MAN_FONT_VAL, 1024);
        lv_label_set_static_text(pedometer_label, pedometer_label_buffer);
    }
}

void test_task(struct _lv_task_t *obj)
{
    static uint16_t height_val=0;
    set_height(height_val);
    height_val += 50;

    static uint8_t date_d_val = 0;
    date_d_val++;
    date_d_val = date_d_val%32;
    set_date(2020, 11, date_d_val);

    static uint8_t time_m_val = 0;
    time_m_val++;
    time_m_val = time_m_val%60;
    set_time(18, time_m_val);

    static uint16_t pedometer_val = 0;
    pedometer_val += 50;
    set_pedometer(pedometer_val);

    static float voltage = 1.0;
    voltage += 0.5;
    set_voltage(voltage);
    if(voltage > 5.0)
    {
        voltage = 1.0;
    }

}

void ui_home_page_start(void)
{
    if(NULL == main_task)
    {
        main_task = lv_task_create(test_task, 500,  LV_TASK_PRIO_LOW, NULL);
        lv_obj_move_foreground(main_view);
    }
}

void ui_home_page_stop(void)
{
    if(NULL != main_task)
    {
        lv_task_del(main_task);
        main_task = NULL;
        lv_obj_move_background(main_view);
    }
}


lv_obj_t *home_page_create(lv_obj_t *parent)
{
    lv_coord_t height;
    lv_coord_t width;

    gauge_style_init();

    /*style初始化*/
    ////////////////////////////////////////////////////////////////////////
    /*main背景*/
    lv_style_copy(&main_background_style, &lv_style_plain_color);
    main_background_style.body.main_color = LV_COLOR_MAKE(122,158,250);
    main_background_style.body.grad_color = LV_COLOR_MAKE(178,96,174);
    main_background_style.body.padding.bottom = 0;

    /*时钟容器背景*/
    lv_style_copy(&time_disp_background_style, &lv_style_plain_color);
    time_disp_background_style.body.main_color = LV_COLOR_WHITE;
    time_disp_background_style.body.grad_color = LV_COLOR_WHITE;
    time_disp_background_style.body.opa = 75;
    time_disp_background_style.body.radius = 10;

    /*功能容器背景view*/
    lv_style_copy(&function_background_style, &time_disp_background_style);

    /*tittle*/
    lv_style_copy(&tittle_background_style, &lv_style_plain_color);
    tittle_background_style.body.opa = 0;
    ///////////////////////////////////////////////////////////////////////

    /*创建main背景view*/
    main_view = main_create(parent, &main_background_style);

    tittle_disp_init(main_view, &tittle_background_style);

    time_disp_init(main_view, &function_background_style);

    /*创建功能 view*/
    function_init(main_view, &time_disp_background_style);


    set_height(0);
    set_date(2020, 11, 13);
    set_time(19, 34);
    set_voltage(3.2);
    set_pedometer(3000);

    //open banck light
    set_lcd_bright(1000);
    
    home_page_cmd.start = ui_home_page_start;
    home_page_cmd.stop = ui_home_page_stop;

    return main_view;
}

